<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /**
     * @param {number} target
     * @param {number} maxDoubles
     * @return {number}
     */
    var minMoves = function (target, maxDoubles) {
      let result = 0;
      // 如果存在可以翻倍的次数，逆向翻倍行动步骤最少
      for (let i = 0; i < maxDoubles; i++) {
        // 如果目标为1，可以直接返回
        if (target === 1) {
          break;
        }
        // 奇数的数，需要递增一次
        if (target % 2 === 1) { result += 1 };
        // 直接翻倍
        target = Math.floor(target / 2);
        result += 1;
      }
      result += target - 1;
      return result;
    };
    console.log(minMoves(target = 5, maxDoubles = 0))
  </script>
</body>

</html>